package com.example.mateproject

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Intent
import android.opengl.Visibility
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation.AnimationListener
import android.view.animation.DecelerateInterpolator
import androidx.activity.ComponentActivity
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.Animation
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.airbnb.lottie.LottieAnimationView
import com.example.mateproject.databinding.ActivityMainLayoutBinding
import com.example.mateproject.ui.activity.TestActivity
import com.example.mateproject.ui.adapter.MainAdapter
import com.example.mateproject.ui.bean.MateBean
import com.example.mateproject.ui.viewmodel.TestViewModel
import com.example.mateproject.ui.viewmodel.ViewModelFactory


class MainActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMainLayoutBinding
    private var adapter:MainAdapter? = null
    private lateinit var testViewModel: TestViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =ActivityMainLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)
        testViewModel = ViewModelProvider(this,ViewModelFactory(application))[TestViewModel::class.java]
        Log.d("TestActivity", "onCreate: $testViewModel")
        testViewModel.position.value = "0"
        initView()
    }

    fun initView(){
        binding.recycler.layoutManager =
            StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
        adapter = MainAdapter(this)
        binding.recycler.adapter = adapter
        binding.btStart?.setOnClickListener {
//            adapter?.setData()
//            startActivity(Intent().apply {
//                setClass(this@MainActivity,TestActivity::class.java)
//                putExtra("current_position",3)
//            })
            testViewModel.position.value = "9"
            startActivity(Intent(this@MainActivity,TestActivity::class.java))
        }
    }

    fun failed(){
        binding.laLoading.visibility = View.VISIBLE
        binding.laLoading.setAnimation(R.raw.animation_fail)
        binding.laLoading.playAnimation()
        binding.laLoading.addAnimatorListener(object: AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                binding.laLoading.visibility = View.GONE
                adapter?.setData()
            }
        })
    }

    fun success(){
        binding.laLoading.visibility = View.VISIBLE
        binding.laLoading.setAnimation(R.raw.animation_successful)
        binding.laLoading.playAnimation()
        binding.laLoading.addAnimatorListener(object: AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                binding.laLoading.visibility = View.GONE
                adapter?.setData()
            }
        })
    }
}